# Makefile.in

abs_top_srcdir	= @abs_top_srcdir@
datarootdir		= @datarootdir@
exec_prefix		= @exec_prefix@
fake_prefix		= @fake_prefix@
installdir		= $(prefix)/$(wireddir)
objdir			= @objdir@
rundir			= @rundir@
mandir			= @mandir@
prefix			= @prefix@
wireddir		= @wireddir@

WD_VERSION		= @WD_VERSION@
WD_MAINTAINER	= @WD_MAINTAINER@
WD_USER			= @WD_USER@
WD_GROUP		= @WD_GROUP@

DISTFILES		= INSTALL LICENSE NEWS README Makefile Makefile.in \
				  config.guess config.status config.h.in config.sub configure \
				  configure.in install-sh libwired man run test wired
SUBDIRS			= libwired

WIREDOBJECTS	= $(addprefix $(objdir)/wired/,$(notdir $(patsubst %.c,%.o,$(shell find $(abs_top_srcdir)/wired -name "[a-z]*.c"))))

DEFS			= @DEFS@ -DWD_ROOT="\"$(fake_prefix)/$(wireddir)\""
CC				= @CC@
CFLAGS			= @CFLAGS@
CPPFLAGS		= @CPPFLAGS@
LDFLAGS			= -L$(rundir)/libwired/lib @LDFLAGS@
LIBS			= -lwired @LIBS@
INCLUDES		= -I$(abs_top_srcdir) -I$(rundir)/libwired/include

INSTALL			= @INSTALL@
COMPILE			= $(CC) $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
PREPROCESS		= $(CC) -E $(DEFS) $(INCLUDES) $(CPPFLAGS) $(CFLAGS)
DEPEND			= $(CC) -MM $(INCLUDES)
LINK			= $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@
ARCHIVE			= ar rcs $@

.PHONY: all all-recursive clean-recursive distclean-recursive install install-only install-wired install-man dist clean distclean scmclean
.NOTPARALLEL:

ifeq ($(WD_MAINTAINER), 1)
ALL				= Makefile configure config.h.in all-recursive $(rundir)/wired $(rundir)/wiredctl $(rundir)/etc/wired.conf $(rundir)/transfertest
else
ALL				= all-recursive $(rundir)/wired $(rundir)/wiredctl $(rundir)/etc/wired.conf
endif

all: $(ALL)

ifeq ($(WD_MAINTAINER), 1)
Makefile: Makefile.in config.status
	./config.status
	        
configure: configure.in
	autoconf

config.h.in: configure.in
	autoheader
	touch $@
	rm -f $@~
endif

all-recursive clean-recursive distclean-recursive:
	@list='$(SUBDIRS)'; \
	for subdir in $$list; do \
		target=`echo $@ | sed s/-recursive//`; \
		(cd $$subdir && $(MAKE) -e $$target) || exit 1; \
	done

$(rundir)/wired: $(WIREDOBJECTS) $(rundir)/libwired/lib/libwired.a
	@test -d $(@D) || mkdir -p $(@D)
	$(LINK) $(WIREDOBJECTS) $(LIBS)

$(rundir)/wiredctl: $(abs_top_srcdir)/wired/wiredctl.in
	@test -d $(@D) || mkdir -p $(@D)
	sed -e 's,@wireddir\@,$(fake_prefix)/$(wireddir),g' $< > $@
	chmod +x $@

$(rundir)/etc/wired.conf: $(abs_top_srcdir)/wired/wired.conf.in
	@test -d $(@D) || mkdir -p $(@D)
	sed -e 's,@WD_USER\@,$(WD_USER),g' -e 's,@WD_GROUP\@,$(WD_GROUP),g' $< > $@

$(objdir)/wired/%.o: $(abs_top_srcdir)/wired/%.c
	@test -d $(@D) || mkdir -p $(@D)
	$(COMPILE) -I$(<D) -c $< -o $@

$(objdir)/wired/%.d: $(abs_top_srcdir)/wired/%.c
	@test -d $(@D) || mkdir -p $(@D)
	($(DEPEND) $< | sed 's,$*.o,$(@D)/&,g'; echo "$@: $<") > $@

$(rundir)/transfertest: $(objdir)/test/transfertest/main.o $(rundir)/libwired/lib/libwired.a
	@test -d $(@D) || mkdir -p $(@D)
	$(LINK) $(objdir)/test/transfertest/main.o $(LIBS)

$(objdir)/test/transfertest/%.o: $(abs_top_srcdir)/test/transfertest/%.c
	@test -d $(@D) || mkdir -p $(@D)
	$(COMPILE) -I$(<D) -c $< -o $@

install: all install-man install-wired

install-only: install-man install-wired

install-wired:
	@if [ -e $(installdir)/wired ]; then \
		touch .update; \
	fi

	$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/
	$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/etc/
	$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/tmp/

	if [ ! -d $(installdir)/files ]; then \
		$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/files/; \
		$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/files/Uploads/; \
		$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/files/Uploads/.wired/; \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/files/Uploads/.wired/type $(installdir)/files/Uploads/.wired/; \
		$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/files/Drop\ Box/; \
		$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) -d $(installdir)/files/Drop\ Box/.wired/; \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/files/Drop\ Box/.wired/type $(installdir)/files/Drop\ Box/.wired/; \
	fi

	if [ ! -f $(installdir)/banlist ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/banlist $(installdir)/; \
	fi

	if [ ! -f $(installdir)/groups ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/groups $(installdir)/; \
	fi

	if [ ! -f $(installdir)/news ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/news $(installdir)/; \
	fi

	if [ ! -f $(installdir)/users ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/users $(installdir)/; \
	fi

	if [ ! -f $(installdir)/etc/wired.conf ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/etc/wired.conf $(installdir)/etc/; \
	fi

	$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/wired $(installdir)/
	$(INSTALL) -m 755 -o $(WD_USER) -g $(WD_GROUP) $(rundir)/wiredctl $(installdir)/

	if [ -f /etc/resolv.conf ]; then \
		$(INSTALL) -m 644 -o $(WD_USER) -g $(WD_GROUP) /etc/resolv.conf $(installdir)/etc/; \
	fi

	@if [ -f .update ]; then \
		echo ""; \
		echo "Update complete!"; \
		echo ""; \
		echo "You should now run:"; \
		echo "    $(installdir)/wiredctl restart"; \
		echo "to restart a running server."; \
	else \
		echo ""; \
		echo "Installation complete!"; \
		echo ""; \
		echo "An administrator account with login \"admin\" and no password has been created."; \
		echo ""; \
		echo "Remember to edit $(installdir)/etc/wired.conf if you want to make any changes before starting the server."; \
		echo ""; \
		echo "When you are done, run:"; \
		echo "    $(installdir)/wiredctl start"; \
		echo "to start the server."; \
	fi

	@rm -f .update

install-man:
	$(INSTALL) -m 755 -d $(mandir)/man1/
	$(INSTALL) -m 644 $(abs_top_srcdir)/man/wiredctl.1 $(mandir)/man1/
	$(INSTALL) -m 755 -d $(mandir)/man5/
	$(INSTALL) -m 644 $(abs_top_srcdir)/man/wired.conf.5 $(mandir)/man5/
	$(INSTALL) -m 755 -d $(mandir)/man8/
	$(INSTALL) -m 644 $(abs_top_srcdir)/man/wired.8 $(mandir)/man8/

dist:
	rm -rf wired-$(WD_VERSION)
	rm -f wired-$(WD_VERSION).tar.gz
	mkdir wired-$(WD_VERSION)

	@for i in $(DISTFILES); do \
		if [ -e $$i ]; then \
			echo cp -LRp $$i wired-$(WD_VERSION)/$$i; \
			cp -LRp $$i wired-$(WD_VERSION)/$$i; \
		fi \
	done

	$(SHELL) -ec "cd wired-$(WD_VERSION) && WD_MAINTAINER=0 WI_MAINTAINER=0 $(MAKE) -e distclean scmclean"

	tar -czf wired-$(WD_VERSION).tar.gz wired-$(WD_VERSION)
	rm -rf wired-$(WD_VERSION)

clean: clean-recursive
	rm -f $(objdir)/wired/*.d
	rm -f $(objdir)/wired/*.o
	rm -f $(objdir)/test/transfertest/*.d
	rm -f $(objdir)/test/transfertest/*.o
	rm -f $(rundir)/wired
	rm -f $(rundir)/wiredctl
	rm -f $(rundir)/etc/wired.conf

distclean: clean distclean-recursive
	rm -rf $(objdir)
	rm -f Makefile config.h config.log config.status config.cache
	rm -f wired-$(WD_VERSION).tar.gz

scmclean:
	find . -name .DS_Store -print0 | xargs -0 rm -f
	find . -name CVS -print0 | xargs -0 rm -rf
	find . -name .svn -print0 | xargs -0 rm -rf

ifeq ($(WD_MAINTAINER), 1)
-include $(WIREDOBJECTS:.o=.d)
endif
